import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

//递归
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null || q == null) {
            return p == q;
        }
        return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

//迭代
class Solution1 {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null || q == null) {
            return p == q;
        }
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(p);
        queue.offer(q);
        while (!queue.isEmpty()) {
            TreeNode curQ = queue.poll();
            TreeNode curP = queue.poll();
            if (curP.val != curQ.val) {
                return false;
            }
            if (curP.left != null && curQ.left != null) {
                queue.offer(curP.left);
                queue.offer(curQ.left);
            } else if (curP.left != curQ.left) {
                return false;
            }

            if (curP.right != null && curQ.right != null) {
                queue.offer(curP.right);
                queue.offer(curQ.right);
            } else if (curP.right != curQ.right) {
                return false;
            }
        }
        return true;
    }
}